package org.particlethink.mapper.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.particlethink.exception.TokenException;
import org.particlethink.model.Supplier;
import org.particlethink.utils.PathUtil;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProSupplierRequestHelper {

    private final static String _url = "/material/supplier/list";
    private final static ObjectMapper objectMapper = new ObjectMapper();

    public static List<Supplier> request(String url, String resourceId, String name, Integer pageNum, Integer pageSize, String token) throws URISyntaxException, IOException {
        url = PathUtil.mergeUrlPaths(url, _url);

        Map<String, Object> params = new HashMap<>();
        if (resourceId != null && !resourceId.isEmpty()) {
            params.put("resourceId", resourceId);
        }
        if (name != null && !name.isEmpty()) {
            params.put("name", name);
        }
        if (pageNum != null) {
            params.put("pageNum", pageNum);
        }
        if (pageSize != null) {
            params.put("pageSize", pageSize);
        }

        List<Supplier> suppliers = new ArrayList<>();

        try (HttpResponse res = HttpUtil.createPost(url).body(objectMapper.writeValueAsString(params), "application/json").header("Authorization", "Bearer " + token).execute()) {
            Response response = objectMapper.readValue(res.bodyBytes(), Response.class);
            if (response.code == 401) {
                throw new TokenException();
            }

            response.getRows().forEach(row -> suppliers.add(new Supplier(row.getId(), null, null, row.getName(), row.getResourceId())));
        }


        return suppliers;
    }

    public static class Request {
        private String resourceId;
        private String name;
        private Integer pageNum;
        private Integer pageSize;

        public Request() {
        }

        public Request(String resourceId, String name, Integer pageNum, Integer pageSize) {
            this.resourceId = resourceId;
            this.name = name;
            this.pageNum = pageNum;
            this.pageSize = pageSize;
        }

        public String getResourceId() {
            return resourceId;
        }

        public void setResourceId(String resourceId) {
            this.resourceId = resourceId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getPageNum() {
            return pageNum;
        }

        public void setPageNum(Integer pageNum) {
            this.pageNum = pageNum;
        }

        public Integer getPageSize() {
            return pageSize;
        }

        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    }

    public static class Response {
        private Integer total;
        private Integer code;
        private String msg;

        private List<Row> rows;

        public Integer getTotal() {
            return total;
        }

        public void setTotal(Integer total) {
            this.total = total;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public List<Row> getRows() {
            return rows;
        }

        public void setRows(List<Row> rows) {
            this.rows = rows;
        }
    }

    public static class Row {
        private Integer id;
        private String resourceId;
        private String name;
        private String address;
        private String phone;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getResourceId() {
            return resourceId;
        }

        public void setResourceId(String resourceId) {
            this.resourceId = resourceId;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }
    }


    public static class SupplierResponse {
        private Integer total;
        private Integer code;
        private String msg;

        private List<Row> data;

        public Integer getTotal() {
            return total;
        }

        public void setTotal(Integer total) {
            this.total = total;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }


        public List<Row> getData() {
            return data;
        }

        public void setData(List<Row> data) {
            this.data = data;
        }
    }
}
